package com.example.controller;


import cn.hutool.json.JSONObject;
import com.example.log.annotation.LogOperation;
import com.example.model.api.Result;
import com.example.security.UserDetail;
import com.example.utils.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@Tag(name = "查看登录用户权限")
@RestController
@RequestMapping("authentication")
public class AuthenticationController {
    @Operation(summary = "详情")
//    @LogOperation("测试日志")
    @GetMapping
    public Result<UserDetail> authentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtAuthenticationToken token = (JwtAuthenticationToken) authentication;
        Object o = token.getTokenAttributes().get("userDetail");
        UserDetail convert = JSON.convert(o, UserDetail.class);
        return new Result<UserDetail>().ok(convert);
    }

    @Operation(summary = "详情")
//    @LogOperation("测试日志")
    @PostMapping
    public Result<UserDetail> postauthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        JwtAuthenticationToken token = (JwtAuthenticationToken) authentication;
        Object o = token.getTokenAttributes().get("userDetail");
        UserDetail convert = JSON.convert(o, UserDetail.class);
        return new Result<UserDetail>().ok(convert);
    }
}
